using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	[TemplatePart(Name = "BackTitleBlock", Type = typeof(TextBlock)), TemplateVisualState(Name = "MsgShow", GroupName = "ImageState"), TemplateVisualState(Name = "HeadImg", GroupName = "ImageState")]
	public class MMHubTile : Control
	{
		private const string ImageStates = "ImageState";
		private const string StrHeadImg = "HeadImg";
		private const string StrMsgShow = "MsgShow";
		private const string BackTitleBlock = "BackTitleBlock";
		private TextBlock _backTitleBlock;
		internal int _stallingCounter;
		internal bool _canFlip;
		public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(MMHubTile), new PropertyMetadata(null, new PropertyChangedCallback(MMHubTile.OnSourceChanged)));
		public static readonly DependencyProperty UnReadCountProperty = DependencyProperty.Register("UnReadCount", typeof(int), typeof(MMHubTile), new PropertyMetadata(0, new PropertyChangedCallback(MMHubTile.UnReadCountChanged)));
		public static readonly DependencyProperty IsFrozenProperty = DependencyProperty.Register("IsFrozen", typeof(bool), typeof(MMHubTile), new PropertyMetadata(false, new PropertyChangedCallback(MMHubTile.OnIsFrozenChanged)));
		public static readonly DependencyProperty GroupTagProperty = DependencyProperty.Register("GroupTag", typeof(string), typeof(MMHubTile), new PropertyMetadata(string.Empty));
		private static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(ImageState), typeof(MMHubTile), new PropertyMetadata(ImageState.HeadImg, new PropertyChangedCallback(MMHubTile.OnImageStateChanged)));
		private Grid BackPanel;
		private bool _contentLoaded;
		public ImageSource Source
		{
			get
			{
				return (ImageSource)base.GetValue(MMHubTile.SourceProperty);
			}
			set
			{
				base.SetValue(MMHubTile.SourceProperty, value);
			}
		}
		public int UnReadCount
		{
			get
			{
				return (int)base.GetValue(MMHubTile.UnReadCountProperty);
			}
			set
			{
				base.SetValue(MMHubTile.UnReadCountProperty, value);
			}
		}
		public bool IsFrozen
		{
			get
			{
				return (bool)base.GetValue(MMHubTile.IsFrozenProperty);
			}
			set
			{
				base.SetValue(MMHubTile.IsFrozenProperty, value);
			}
		}
		public string GroupTag
		{
			get
			{
				return (string)base.GetValue(MMHubTile.GroupTagProperty);
			}
			set
			{
				base.SetValue(MMHubTile.GroupTagProperty, value);
			}
		}
		internal ImageState State
		{
			get
			{
				return (ImageState)base.GetValue(MMHubTile.StateProperty);
			}
			set
			{
				base.SetValue(MMHubTile.StateProperty, value);
			}
		}
		private static void OnSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			MMHubTile mMHubTile = (MMHubTile)dependencyObject_0;
			if (dependencyPropertyChangedEventArgs_0.get_NewValue() == null)
			{
				mMHubTile.RefreshSourceAndCount();
				return;
			}
			if (dependencyPropertyChangedEventArgs_0.get_NewValue() != dependencyPropertyChangedEventArgs_0.get_OldValue())
			{
				mMHubTile.RefreshSourceAndCount();
			}
		}
		private void RefreshSourceAndCount()
		{
			if (this.Source == null)
			{
				this._canFlip = false;
				this.State = ImageState.HeadImg;
				if (this.BackPanel != null)
				{
					this.BackPanel.set_Visibility(1);
				}
				return;
			}
			if (this.BackPanel != null)
			{
				this.BackPanel.set_Visibility(0);
			}
			if (this.UnReadCount <= 0)
			{
				this._canFlip = false;
				this.State = ImageState.HeadImg;
				if (this.BackPanel != null)
				{
					this.BackPanel.set_Visibility(1);
				}
				return;
			}
			this._canFlip = true;
			this.State = ImageState.HeadImg;
		}
		private static void UnReadCountChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			MMHubTile mMHubTile = (MMHubTile)dependencyObject_0;
			mMHubTile.RefreshSourceAndCount();
		}
		private static void OnIsFrozenChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			MMHubTile tile = (MMHubTile)dependencyObject_0;
			if ((bool)dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				MMHubTileService.FreezeHubTile(tile);
				return;
			}
			MMHubTileService.UnfreezeHubTile(tile);
		}
		private static void OnImageStateChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			((MMHubTile)dependencyObject_0).UpdateVisualState();
		}
		private void UpdateVisualState()
		{
			string text;
			switch (this.State)
			{
			case ImageState.HeadImg:
				text = "HeadImg";
				break;
			case ImageState.MsgShow:
				text = "MsgShow";
				break;
			default:
				text = "HeadImg";
				break;
			}
			VisualStateManager.GoToState(this, text, true);
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this._backTitleBlock = (base.GetTemplateChild("BackTitleBlock") as TextBlock);
			this.BackPanel = (base.GetTemplateChild("BackPanel") as Grid);
			if (this._backTitleBlock != null)
			{
				Binding binding = new Binding();
				binding.set_Source(this);
				binding.set_Path(new PropertyPath("UnReadCount", new object[0]));
				binding.set_Converter(new UnreadConverter());
				this._backTitleBlock.SetBinding(TextBlock.TextProperty, binding);
			}
			this.RefreshSourceAndCount();
			this.UpdateVisualState();
		}
		public MMHubTile()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.HubTile_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.HubTile_Unloaded));
		}
		private void HubTile_Loaded(object sender, RoutedEventArgs e)
		{
			MMHubTileService.InitializeReference(this);
		}
		private void HubTile_Unloaded(object sender, RoutedEventArgs e)
		{
			MMHubTileService.FinalizeReference(this);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Session/MMHubTile/MMHubTile.xaml", 2));
		}
	}
}
